package me.hl.exception;

import org.junit.Test;

import java.io.ByteArrayOutputStream;

/**
 * Created by Planck on 2016/2/15.
 */
public class ExceptionsDemo {

    @Test
    //抛出一个异常，并且输出这个异常
    public void exceptionDemo() {
        try {
            String str = null;
            str.substring(1);
        } catch (Exception e) {
            ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
            e.printStackTrace(new java.io.PrintWriter(buf, true));
            String expMessage = buf.toString();
            System.out.println(expMessage);
        }
    }

    public void throwException() {
        try {
            String str = null;
            str.charAt(1);
        } catch (Exception e) {
            throw new MyException("1001", e.getMessage());
        }
    }

    @Test
    public void myCatch() {
        try {
            throwException();
        } catch (MyException e) {
            e.printStackTrace();
            System.out.println(e.getRetCode());
        }
    }

}
